#include "stdafx.h"
#include "DivisionOperationTreeNode.h"
#include "exceptions.h"
#include "ComputationalTreeNodeVisitor.h"

using CollabRC::Bot::AI::DivisionOperationTreeNode;
using CollabRC::Bot::AI::ComputationalTreeNode;
DivisionOperationTreeNode::Factory DivisionOperationTreeNode::factory;

/** @brief Destroys this DivisionOperationTreeNode and all associated resources */
DivisionOperationTreeNode::~DivisionOperationTreeNode(void)
{
}

/**
 * @brief Creates a new DivisionOperationTreeNode acting upon the specified branches
 * @param lhs the left hand branch upon which to perform the operation
 * @param the right hand branch upon which to perform the operation
 */
CollabRC::Bot::AI::DivisionOperationTreeNode::DivisionOperationTreeNode( const ComputationalTreeNode& lhs, const ComputationalTreeNode& rhs )
:OperationTreeNode(lhs, rhs) { }

/**
 * @brief Creates a new DivisionOperationTreeNode acting upon the branches formerly
 * acted upon by the specified OperationTreeNode
 * @param copy the operation from which this division operation is being copied
 */
CollabRC::Bot::AI::DivisionOperationTreeNode::DivisionOperationTreeNode( const OperationTreeNode& copy )
:OperationTreeNode(copy) { }

/** {@inheritDoc} */
CollabRC::Bot::AI::ComputationalTreeNode* DivisionOperationTreeNode::Clone() const
{
    return new DivisionOperationTreeNode(*this);
}

/** {@inheritDoc} */
float CollabRC::Bot::AI::DivisionOperationTreeNode::PerformOperation( float lhs, float rhs ) const
{
    if (rhs == 0)   // Division by zero
    {
        IllegalOperationException ex("Attempt to divide by zero");
        throw ex;
    }
    else 
    {   
        return lhs / rhs;
    }
}
/**
 * {@inheritDoc}
 */
CollabRC::Bot::AI::ComputationalTreeNode* CollabRC::Bot::AI::DivisionOperationTreeNode::Factory::Instantiate( const ComputationalTreeNode& lhs, const ComputationalTreeNode &rhs ) const
{
    return new CollabRC::Bot::AI::DivisionOperationTreeNode(lhs, rhs);
}

void CollabRC::Bot::AI::DivisionOperationTreeNode::Accept(CollabRC::Bot::AI::ComputationalTreeNodeVisitor &visitor)
{
    OperationTreeNode::Accept(visitor);
    visitor.Visit(*this);
}
